home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr25 / me310.zip / UE310C.ZIP / MAIN.C < prev    next >
Text File  |  1989-10-14  |  22KB  |  949 lines

  1. /*
  2.  *    MicroEMACS 3.10
  3.  *        written by Daniel M. Lawrence
  4.  *        based on code by Dave G. Conroy.
  5.  *
  6.  *    (C)opyright 1988,1989 by Daniel M. Lawrence
  7.  *    MicroEMACS 3.10 can be copied and distributed freely for any
  8.  *    non-commercial purposes. MicroEMACS 3.10 can only be incorporated
  9.  *    into commercial software with the permission of the current author.
  10.  *
  11.  * This file contains the main driving routine, and some keyboard processing
  12.  * code, for the MicroEMACS screen editor.
  13.  *
  14.  */
  15.  
  16. #include    <stdio.h>
  17.  
  18. /* make global definitions not external */
  19. #define maindef
  20.  
  21. #include    "estruct.h"    /* global structures and defines */
  22. #include    "etype.h"    /* variable prototype definitions */
  23. #include    "efunc.h"    /* function declarations and name table */
  24. #include    "edef.h"    /* global definitions */
  25. #include    "elang.h"    /* human language definitions */
  26. #include    "ebind.h"    /* default key bindings */
  27.  
  28. /* for many different systems, increase the default stack space */
  29.  
  30. #if    MSDOS & LATTICE
  31. unsigned _stack = 20000;
  32. #endif
  33.  
  34. #if    MSDOS & DTL
  35. int    _okbigbuf = 0;        /* Only allocate memory when needed.*/
  36. int    _stack = 20000;     /* Reset the ol' stack size.*/
  37. #endif
  38.  
  39. #if    ATARI & MWC
  40. long _stksize = 20000L;     /* reset stack size (must be even) */
  41. #endif
  42.  
  43. #if    MSDOS & AZTEC
  44. int _STKSIZ = 20000/16;     /* stack size in paragraphs */
  45. int _STKRED = 1024;        /* stack checking limit */
  46. int _HEAPSIZ = 4096/16;     /* (in paragraphs) */
  47. /*int _STKLOW = 0;        default is stack above heap (small only) */
  48. #endif
  49.  
  50. #if    MSDOS & TURBO
  51. extern unsigned int _stklen = 10000;
  52. #endif
  53.  
  54. /*    make VMS happy...    */
  55.  
  56. #if    VMS
  57. #include    <ssdef.h>
  58. #define GOOD    (SS$_NORMAL)
  59. #endif
  60.  
  61. #ifndef GOOD
  62. #define GOOD    0
  63. #endif
  64.  
  65. /*
  66.     This is the primary entry point that is used by command line
  67.     invocation, and by applications that link with microemacs in
  68.     such a way that each invocation of Emacs is a fresh environment.
  69.  
  70.     There is another entry point in VMS.C that is used when
  71.     microemacs is "linked" (In quotes, because it is a run-time link
  72.     rather than a link-time link.) with applications that wish Emacs
  73.     to preserve it's context across invocations.  (For example,
  74.     EMACS.RC is only executed once per invocation of the
  75.     application, instead of once per invocation of Emacs.)
  76.  
  77.     Note that re-entering an Emacs that is saved in a kept
  78.     subprocess would require a similar entrypoint.
  79. */
  80.  
  81. #if    CALLED
  82. emacs(argc, argv)
  83. #else
  84. main(argc, argv)
  85. #endif
  86.  
  87. int argc;    /* # of arguments */
  88. char *argv[];    /* argument strings */
  89.  
  90. {
  91.     register int status;
  92.  
  93.     /* Initialize the editor */
  94.     eexitflag = FALSE;
  95.     vtinit();        /* Terminal */
  96.     if (eexitflag)
  97.         goto abortrun;
  98.     edinit("main");     /* Buffers, windows */
  99.     varinit();        /* user variables */
  100.     initchars();        /* character set definitions */
  101.  
  102.     /* Process the command line and let the user edit */
  103.     dcline(argc, argv);
  104.     status = editloop();
  105. abortrun:
  106.     vttidy();
  107. #if    CLEAN
  108.     clean();
  109. #endif
  110. #if    CALLED
  111.     return(status);
  112. #else
  113.     exit(status);
  114. #endif
  115. }
  116.  
  117. #if    CLEAN
  118. /*
  119.     On some primitive operation systems, and when emacs is used as
  120.     a subprogram to a larger project, emacs needs to de-alloc its
  121.     own used memory, otherwise we just exit.
  122. */
  123.  
  124. PASCAL NEAR clean()
  125. {
  126.     register BUFFER *bp;    /* buffer list pointer */
  127.     register WINDOW *wp;    /* window list pointer */
  128.     register WINDOW *tp;    /* temporary window pointer */
  129.  
  130.     /* first clean up the windows */
  131.     wp = wheadp;
  132.     while (wp) {
  133.         tp = wp->w_wndp;
  134.         free(wp);
  135.         wp = tp;
  136.     }
  137.     wheadp = NULL;
  138.  
  139.     /* then the buffers */
  140.     bp = bheadp;
  141.     while (bp) {
  142.         bp->b_nwnd = 0;
  143.         bp->b_flag = 0; /* don't say anything about a changed buffer! */
  144.         zotbuf(bp);
  145.         bp = bheadp;
  146.     }
  147.  
  148.     /* and the kill buffer */
  149.     kdelete();
  150.  
  151.     /* clear some search variables */
  152. #if    MAGIC
  153.     mcclear();
  154.     rmcclear();
  155. #endif
  156.     if (patmatch != NULL) {
  157.         free(patmatch);
  158.         patmatch = NULL;
  159.     }
  160.  
  161.     /* dealloc the user variables */
  162.     varclean();
  163.  
  164.     /* and the video buffers */
  165.     vtfree();
  166. }
  167. #endif
  168.  
  169. /*    Process a command line.   May be called any time.    */
  170.  
  171. PASCAL NEAR dcline(argc, argv)
  172.  
  173. int argc;
  174. char *argv[];
  175.  
  176. {
  177.     register BUFFER *bp;        /* temp buffer pointer */
  178.     register int    firstfile;    /* first file flag */
  179.     register int    carg;        /* current arg to scan */
  180.     register int    startflag;    /* startup executed flag */
  181.     BUFFER *firstbp = NULL;     /* ptr to first buffer in cmd line */
  182.     int viewflag;            /* are we starting in view mode? */
  183.     int gotoflag;            /* do we need to goto a line at start? */
  184.     int gline;            /* if so, what line? */
  185.     int searchflag;         /* Do we need to search at start? */
  186.     int errflag;            /* C error processing? */
  187.     VDESC vd;            /* variable num/type */
  188.     char bname[NBUFN];        /* buffer name of file to read */
  189.  
  190. #if    CRYPT
  191.     int cryptflag;            /* encrypting on the way in? */
  192.     char ekey[NPAT];        /* startup encryption key */
  193. #endif
  194.     CONST NOSHARE extern *pathname[];    /* startup file path/name array */
  195.  
  196.     viewflag = FALSE;    /* view mode defaults off in command line */
  197.     gotoflag = FALSE;    /* set to off to begin with */
  198.     searchflag = FALSE;    /* set to off to begin with */
  199.     firstfile = TRUE;    /* no file to edit yet */
  200.     startflag = FALSE;    /* startup file not executed yet */
  201.     errflag = FALSE;    /* not doing C error parsing */
  202. #if    CRYPT
  203.     cryptflag = FALSE;    /* no encryption by default */
  204. #endif
  205.     /* Parse a command line */
  206.     for (carg = 1; carg < argc; ++carg) {
  207.  
  208.         /* Process Switches */
  209. #if WMCS
  210.         if (argv[carg][0] == ':') {
  211. #else
  212.         if (argv[carg][0] == '-') {
  213. #endif
  214.             switch (argv[carg][1]) {
  215.                 /* Process Startup macroes */
  216.                 case 'c':    /* -c for changable file */
  217.                 case 'C':
  218.                     viewflag = FALSE;
  219.                     break;
  220.                 case 'e':    /* -e process error file */
  221.                 case 'E':
  222.                     errflag = TRUE;
  223.                     break;
  224.                 case 'g':    /* -g for initial goto */
  225.                 case 'G':
  226.                     gotoflag = TRUE;
  227.                     gline = asc_int(&argv[carg][2]);
  228.                     break;
  229.                 case 'i':    /* -i<var> <value> set an initial */
  230.                 case 'I':    /* value for a variable */
  231.                     bytecopy(bname, &argv[carg][2], NVSIZE);
  232.                     findvar(bname, &vd, NVSIZE + 1);
  233.                     if (vd.v_type == -1) {
  234.                         mlwrite(TEXT52, bname);
  235. /*                            "%%No such variable as '%s'" */
  236.                         break;
  237.                     }
  238.                     svar(&vd, argv[++carg]);
  239.                     break;
  240. #if    CRYPT
  241.                 case 'k':    /* -k<key> for code key */
  242.                 case 'K':
  243.                     cryptflag = TRUE;
  244.                     strcpy(ekey, &argv[carg][2]);
  245.                     break;
  246. #endif
  247.                 case 'r':    /* -r restrictive use */
  248.                 case 'R':
  249.                     restflag = TRUE;
  250.                     break;
  251.                 case 's':    /* -s for initial search string */
  252.                 case 'S':
  253.                     searchflag = TRUE;
  254.                     bytecopy(pat,&argv[carg][2],NPAT);
  255.                     setjtable(pat);
  256.                     break;
  257.                 case 'v':    /* -v for View File */
  258.                 case 'V':
  259.                     viewflag = TRUE;
  260.                     break;
  261.                 default:    /* unknown switch */
  262.                     /* ignore this for now */
  263.                     break;
  264.             }
  265.  
  266.         } else if (argv[carg][0]== '@') {
  267.  
  268.             /* Process Startup macroes */
  269.             if (startup(&argv[carg][1]) == TRUE)
  270.                 /* don't execute emacs.rc */
  271.                 startflag = TRUE;
  272.  
  273.         } else {
  274.  
  275.             /* Process an input file */
  276.  
  277.             /* set up a buffer for this file */
  278.             makename(bname, argv[carg]);
  279.             unqname(bname);
  280.  
  281.             /* set this to inactive */
  282.             bp = bfind(bname, TRUE, 0);
  283.             strcpy(bp->b_fname, argv[carg]);
  284.             bp->b_active = FALSE;
  285.             if (firstfile) {
  286.                 firstbp = bp;
  287.                 firstfile = FALSE;
  288.             }
  289.  
  290.             /* set the modes appropriatly */
  291.             if (viewflag)
  292.                 bp->b_mode |= MDVIEW;
  293. #if    CRYPT
  294.             if (cryptflag) {
  295.                 bp->b_mode |= MDCRYPT;
  296.                 crypt((char *)NULL, 0);
  297.                 crypt(ekey, strlen(ekey));
  298.                 bytecopy(bp->b_key, ekey, NPAT);
  299.             }
  300. #endif
  301.         }
  302.     }
  303.  
  304.     /* if we are C error parsing... run it! */
  305.     if (errflag) {
  306.         if (startup("error.cmd") == TRUE)
  307.             startflag = TRUE;
  308.     }
  309.  
  310.     /* if invoked with no other startup files,
  311.        run the system startup file here */
  312.     if (startflag == FALSE)
  313.         startup("");
  314.  
  315.     /* if there are any files to read, read the first one! */
  316.     bp = bfind("main", FALSE, 0);
  317.     if (firstfile == FALSE && (gflags & GFREAD)) {
  318.         swbuffer(firstbp);
  319.         curbp->b_mode |= gmode;
  320.         update(TRUE);
  321.         mlwrite(lastmesg);
  322.         zotbuf(bp);
  323.     } else
  324.         bp->b_mode |= gmode;
  325.  
  326.     /* Deal with startup gotos and searches */
  327.     if (gotoflag && searchflag) {
  328.         update(FALSE);
  329.         mlwrite(TEXT101);
  330. /*            "[Can not search and goto at the same time!]" */
  331.     }
  332.     else if (gotoflag) {
  333.         if (gotoline(TRUE,gline) == FALSE) {
  334.             update(FALSE);
  335.             mlwrite(TEXT102);
  336. /*                "[Bogus goto argument]" */
  337.         }
  338.     } else if (searchflag) {
  339.         if (forwhunt(FALSE, 0) == FALSE)
  340.             update(FALSE);
  341.     }
  342.  
  343. }
  344.  
  345. /*
  346.     This is called to let the user edit something.    Note that if you
  347.     arrange to be able to call this from a macro, you will have
  348.     invented the "recursive-edit" function.
  349. */
  350.  
  351. PASCAL NEAR editloop()
  352.  
  353. {
  354.     register int c;         /* command character */
  355.     register int f;     /* default flag */
  356.     register int n;     /* numeric repeat count */
  357.     register int mflag;    /* negative flag on repeat */
  358.     register int basec;    /* c stripped of meta character */
  359.     register int oldflag;    /* old last flag value */
  360.  
  361.     /* setup to process commands */
  362.     lastflag = 0;        /* Fake last flags.    */
  363.  
  364. loop:
  365.     /* if we were called as a subroutine and want to leave, do so */
  366.     if (eexitflag)
  367.         return(eexitval);
  368.  
  369.     /* execute the "command" macro...normally null */
  370.     oldflag = lastflag;    /* preserve lastflag through this */
  371.     execkey(&cmdhook, FALSE, 1);
  372.     lastflag = oldflag;
  373.  
  374.     /* Fix up the screen    */
  375.     update(FALSE);
  376.  
  377.     /* get the next command from the keyboard */
  378.     c = getkey();
  379.  
  380.     /* if there is something on the command line, clear it */
  381.     if (mpresf != FALSE) {
  382.         mlerase();
  383.         update(FALSE);
  384. #if    CLRMSG
  385.         if (c == ' ')            /* ITS EMACS does this    */
  386.             goto loop;
  387. #endif
  388.     }
  389.  
  390.     /* override the arguments if prefixed */
  391.     if (prefix) {
  392.         if (islower(c & 255))
  393.             c = (c & ~255) | upperc(c & 255);
  394.         c |= prefix;
  395.         f = predef;
  396.         n = prenum;
  397.         prefix = 0;
  398.     } else {
  399.         f = FALSE;
  400.         n = 1;
  401.     }
  402.  
  403.     /* do META-# processing if needed */
  404.  
  405.     basec = c & ~META;        /* strip meta char off if there */
  406.     if ((c & META) && ((basec >= '0' && basec <= '9') || basec == '-') &&
  407.         (getbind(c) == NULL)) {
  408.         f = TRUE;        /* there is a # arg */
  409.         n = 0;            /* start with a zero default */
  410.         mflag = 1;        /* current minus flag */
  411.         c = basec;        /* strip the META */
  412.         while ((c >= '0' && c <= '9') || (c == '-')) {
  413.             if (c == '-') {
  414.                 /* already hit a minus or digit? */
  415.                 if ((mflag == -1) || (n != 0))
  416.                     break;
  417.                 mflag = -1;
  418.             } else {
  419.                 n = n * 10 + (c - '0');
  420.             }
  421.             if ((n == 0) && (mflag == -1))    /* lonely - */
  422.                 mlwrite("Arg:");
  423.             else
  424.                 mlwrite("Arg: %d",n * mflag);
  425.  
  426.             c = getkey();    /* get the next key */
  427.         }
  428.         n = n * mflag;    /* figure in the sign */
  429.     }
  430.  
  431.     /* do ^U repeat argument processing */
  432.  
  433.     if (c == reptc) {           /* ^U, start argument   */
  434.         f = TRUE;
  435.         n = 4;                /* with argument of 4 */
  436.         mflag = 0;            /* that can be discarded. */
  437.         mlwrite("Arg: 4");
  438.         while ((c=getkey()) >='0' && c<='9' || c==reptc || c=='-') {
  439.             if (c == reptc)
  440.                 if ((n > 0) == ((n*4) > 0))
  441.                     n = n*4;
  442.                 else
  443.                     n = 1;
  444.             /*
  445.              * If dash, and start of argument string, set arg.
  446.              * to -1.  Otherwise, insert it.
  447.              */
  448.             else if (c == '-') {
  449.                 if (mflag)
  450.                     break;
  451.                 n = 0;
  452.                 mflag = -1;
  453.             }
  454.             /*
  455.              * If first digit entered, replace previous argument
  456.              * with digit and set sign.  Otherwise, append to arg.
  457.              */
  458.             else {
  459.                 if (!mflag) {
  460.                     n = 0;
  461.                     mflag = 1;
  462.                 }
  463.                 n = 10*n + c - '0';
  464.             }
  465.             mlwrite("Arg: %d", (mflag >=0) ? n : (n ? -n : -1));
  466.         }
  467.         /*
  468.          * Make arguments preceded by a minus sign negative and change
  469.          * the special argument "^U -" to an effective "^U -1".
  470.          */
  471.         if (mflag == -1) {
  472.             if (n == 0)
  473.                 n++;
  474.             n = -n;
  475.         }
  476.     }
  477.  
  478.     /* and execute the command */
  479.     execute(c, f, n);
  480.     goto loop;
  481. }
  482.  
  483. /*
  484.  * Initialize all of the buffers and windows. The buffer name is passed down
  485.  * as an argument, because the main routine may have been told to read in a
  486.  * file by default, and we want the buffer name to be right.
  487.  */
  488.  
  489. PASCAL NEAR edinit(bname)
  490.  
  491. char bname[];    /* name of buffer to initialize */
  492.  
  493. {
  494.     register BUFFER *bp;
  495.     register WINDOW *wp;
  496.     int cmark;        /* current mark */
  497.  
  498.     /* initialize some important globals */
  499.  
  500.     readhook.k_ptr.fp = nullproc;    /* set internal hooks to OFF */
  501.     readhook.k_type = BINDFNC;
  502.     wraphook.k_ptr.fp = wrapword;
  503.     wraphook.k_type = BINDFNC;
  504.     cmdhook.k_ptr.fp = nullproc;
  505.     cmdhook.k_type = BINDFNC;
  506.     writehook.k_ptr.fp = nullproc;
  507.     writehook.k_type = BINDFNC;
  508.     bufhook.k_ptr.fp = nullproc;
  509.     bufhook.k_type = BINDFNC;
  510.     exbhook.k_ptr.fp = nullproc;
  511.     exbhook.k_type = BINDFNC;
  512.  
  513.     bp = bfind(bname, TRUE, 0);        /* First buffer     */
  514.     blistp = bfind("[List]", TRUE, BFINVS); /* Buffer list buffer    */
  515.     wp = (WINDOW *) malloc(sizeof(WINDOW)); /* First window     */
  516.     if (bp==NULL || wp==NULL || blistp==NULL)
  517.         meexit(1);
  518.     curbp  = bp;                /* Make this current    */
  519.     wheadp = wp;
  520.     curwp  = wp;
  521.     wp->w_wndp  = NULL;            /* Initialize window    */
  522.     wp->w_bufp  = bp;
  523.     bp->b_nwnd  = 1;            /* Displayed.        */
  524.     wp->w_linep = bp->b_linep;
  525.     wp->w_dotp  = bp->b_linep;
  526.     wp->w_doto  = 0;
  527.     for (cmark = 0; cmark < NMARKS; cmark++) {
  528.         wp->w_markp[cmark] = NULL;
  529.         wp->w_marko[cmark] = 0;
  530.     }
  531.     wp->w_toprow = 0;
  532. #if    COLOR
  533.     /* initalize colors to global defaults */
  534.     wp->w_fcolor = gfcolor;
  535.     wp->w_bcolor = gbcolor;
  536. #endif
  537.     wp->w_fcol = 0;
  538.     wp->w_ntrows = term.t_nrow-1;        /* "-1" for mode line.    */
  539.     wp->w_force = 0;
  540.     wp->w_flag  = WFMODE|WFHARD;        /* Full.        */
  541. }
  542.  
  543. /*
  544.  * This is the general command execution routine. It handles the fake binding
  545.  * of all the keys to "self-insert". It also clears out the "thisflag" word,
  546.  * and arranges to move it to the "lastflag", so that the next command can
  547.  * look at it. Return the status of command.
  548.  */
  549. PASCAL NEAR execute(c, f, n)
  550.  
  551. {
  552.     register int status;
  553.     KEYTAB *key;            /* key entry to execute */
  554.  
  555.     /* if the keystroke is a bound function...do it */
  556.     key = getbind(c);
  557.     if (key != NULL) {
  558.         thisflag = 0;
  559.         status = execkey(key, f, n);
  560.         lastflag = thisflag;
  561.         return(status);
  562.     }
  563.  
  564.     /*
  565.      * If a space was typed, fill column is defined, the argument is non-
  566.      * negative, wrap mode is enabled, and we are now past fill column,
  567.      * and we are not read-only, perform word wrap.
  568.      */
  569.     if (c == ' ' && (curwp->w_bufp->b_mode & MDWRAP) && fillcol > 0 &&
  570.         n >= 0 && getccol(FALSE) > fillcol &&
  571.         (curwp->w_bufp->b_mode & MDVIEW) == FALSE)
  572.         execkey(&wraphook, FALSE, 1);
  573.  
  574.     if ((c>=0x20 && c<=0xFF)) {    /* Self inserting.    */
  575.         if (n <= 0) {            /* Fenceposts.        */
  576.             lastflag = 0;
  577.             return(n<0 ? FALSE : TRUE);
  578.         }
  579.         thisflag = 0;            /* For the future.    */
  580.  
  581.         /* if we are in overwrite mode, not at eol,
  582.            and next char is not a tab or we are at a tab stop,
  583.            delete a char forword            */
  584.         if (curwp->w_bufp->b_mode & MDOVER &&
  585.             curwp->w_doto < curwp->w_dotp->l_used &&
  586.             (lgetc(curwp->w_dotp, curwp->w_doto) != '\t' ||
  587.              (curwp->w_doto) % tabsize == (tabsize - 1)))
  588.                 ldelete(1L, FALSE);
  589.  
  590.         /* do the appropriate insertion */
  591.         if (c == '}' && (curbp->b_mode & MDCMOD) != 0)
  592.             status = insbrace(n, c);
  593.         else if (c == '#' && (curbp->b_mode & MDCMOD) != 0)
  594.             status = inspound();
  595.         else
  596.             status = linsert(n, c);
  597.  
  598. #if    CFENCE
  599.         /* check for CMODE fence matching */
  600.         if ((c == '}' || c == ')' || c == ']') &&
  601.                 (curbp->b_mode & MDCMOD) != 0)
  602.             fmatch(c);
  603. #endif
  604.  
  605.         /* check auto-save mode */
  606.         if (curbp->b_mode & MDASAVE)
  607.             if (--gacount == 0) {
  608.                 /* and save the file if needed */
  609.                 upscreen(FALSE, 0);
  610.                 filesave(FALSE, 0);
  611.                 gacount = gasave;
  612.             }
  613.  
  614.         lastflag = thisflag;
  615.         return(status);
  616.     }
  617.     TTbeep();
  618.     mlwrite(TEXT19);        /* complain        */
  619. /*        "[Key not bound]" */
  620.     lastflag = 0;                /* Fake last flags.    */
  621.     return(FALSE);
  622. }
  623.  
  624. /*
  625.     Fancy quit command, as implemented by Norm. If the any buffer
  626. has changed do a write on that buffer and exit emacs, otherwise simply
  627. exit.
  628. */
  629.  
  630. PASCAL NEAR quickexit(f, n)
  631.  
  632. {
  633.     register BUFFER *bp;    /* scanning pointer to buffers */
  634.     register BUFFER *oldcb; /* original current buffer */
  635.     register int status;
  636.  
  637.     oldcb = curbp;                /* save in case we fail */
  638.  
  639.     bp = bheadp;
  640.     while (bp != NULL) {
  641.         if ((bp->b_flag&BFCHG) != 0    /* Changed.        */
  642.         && (bp->b_flag&BFINVS) == 0) {    /* Real.        */
  643.             curbp = bp;        /* make that buffer cur */
  644.             mlwrite(TEXT103,bp->b_fname);
  645. /*                "[Saving %s]" */
  646.             mlwrite("\n");
  647.             if ((status = filesave(f, n)) != TRUE) {
  648.                 curbp = oldcb;    /* restore curbp */
  649.                 return(status);
  650.             }
  651.         }
  652.     bp = bp->b_bufp;            /* on to the next buffer */
  653.     }
  654.     quit(f, n);                /* conditionally quit    */
  655.     return(TRUE);
  656. }
  657.  
  658. /*
  659.  * Quit command. If an argument, always quit. Otherwise confirm if a buffer
  660.  * has been changed and not written out. Normally bound to "C-X C-C".
  661.  */
  662.  
  663. PASCAL NEAR quit(f, n)
  664.  
  665. {
  666.     register int status;    /* return status */
  667.  
  668.     if (f != FALSE        /* Argument forces it.    */
  669.     || anycb() == FALSE    /* All buffers clean or user says it's OK. */
  670.     || (status = mlyesno(TEXT104)) == TRUE) {
  671. /*                 "Modified buffers exist. Leave anyway" */
  672. #if    FILOCK
  673.         if (lockrel() != TRUE) {
  674.             TTputc('\n');
  675.             TTputc('\r');
  676.             TTclose();
  677.             TTkclose();
  678.             status = meexit(1);
  679.         }
  680. #endif
  681.         if (f)
  682.             status = meexit(n);
  683.         else
  684.             status = meexit(GOOD);
  685.     }
  686.     mlerase();
  687.     return(status);
  688. }
  689.  
  690. PASCAL NEAR meexit(status)
  691. int status;    /* return status of emacs */
  692. {
  693.     eexitflag = TRUE;    /* flag a program exit */
  694.     eexitval = status;
  695.  
  696.     /* and now.. we leave and let the main loop kill us */
  697.     return(TRUE);
  698. }
  699.  
  700. /*
  701.  * Begin a keyboard macro.
  702.  * Error if not at the top level in keyboard processing. Set up variables and
  703.  * return.
  704.  */
  705.  
  706. PASCAL NEAR ctlxlp(f, n)
  707.  
  708. {
  709.     if (kbdmode != STOP) {
  710.         mlwrite(TEXT105);
  711. /*            "%%Macro already active" */
  712.         return(FALSE);
  713.     }
  714.     mlwrite(TEXT106);
  715. /*        "[Start macro]" */
  716.     kbdptr = &kbdm[0];
  717.     kbdend = kbdptr;
  718.     kbdmode = RECORD;
  719.     return(TRUE);
  720. }
  721.  
  722. /*
  723.  * End keyboard macro. Check for the same limit conditions as the above
  724.  * routine. Set up the variables and return to the caller.
  725.  */
  726.  
  727. PASCAL NEAR ctlxrp(f, n)
  728.  
  729. {
  730.     if (kbdmode == STOP) {
  731.         mlwrite(TEXT107);
  732. /*            "%%Macro not active" */
  733.         return(FALSE);
  734.     }
  735.     if (kbdmode == RECORD) {
  736.         mlwrite(TEXT108);
  737. /*            "[End macro]" */
  738.         kbdmode = STOP;
  739.     }
  740.     return(TRUE);
  741. }
  742.  
  743. /*
  744.  * Execute a macro.
  745.  * The command argument is the number of times to loop. Quit as soon as a
  746.  * command gets an error. Return TRUE if all ok, else FALSE.
  747.  */
  748.  
  749. PASCAL NEAR ctlxe(f, n)
  750.  
  751. {
  752.     if (kbdmode != STOP) {
  753.         mlwrite(TEXT105);
  754. /*            "%%Macro already active" */
  755.         return(FALSE);
  756.     }
  757.     if (n <= 0)
  758.         return(TRUE);
  759.     kbdrep = n;        /* remember how many times to execute */
  760.     kbdmode = PLAY;     /* start us in play mode */
  761.     kbdptr = &kbdm[0];    /*    at the beginning */
  762.     return(TRUE);
  763. }
  764.  
  765. /*
  766.  * Abort.
  767.  * Beep the beeper. Kill off any keyboard macro, etc., that is in progress.
  768.  * Sometimes called as a routine, to do general aborting of stuff.
  769.  */
  770.  
  771. PASCAL NEAR ctrlg(f, n)
  772.  
  773. {
  774.     TTbeep();
  775.     kbdmode = STOP;
  776.     mlwrite(TEXT8);
  777. /*        "[Aborted]" */
  778.     return(ABORT);
  779. }
  780.  
  781. /* tell the user that this command is illegal while we are in
  782.    VIEW (read-only) mode                */
  783.  
  784. PASCAL NEAR rdonly()
  785.  
  786. {
  787.     TTbeep();
  788.     mlwrite(TEXT109);
  789. /*        "[Key illegal in VIEW mode]" */
  790.     return(FALSE);
  791. }
  792.  
  793. PASCAL NEAR resterr()
  794.  
  795. {
  796.     TTbeep();
  797.     mlwrite(TEXT110);
  798. /*        "[That command is RESTRICTED]" */
  799.     return(FALSE);
  800. }
  801.  
  802. PASCAL NEAR nullproc(f, n)    /* user function that does NOTHING */
  803.  
  804. int n, f;    /* yes, these are default and never used.. but MUST be here */
  805.  
  806. {
  807. }
  808.  
  809. PASCAL NEAR meta(f, n)    /* set META prefixing pending */
  810.  
  811. {
  812.     prefix |= META;
  813.     prenum = n;
  814.     predef = f;
  815.     return(TRUE);
  816. }
  817.  
  818. PASCAL NEAR cex(f, n)    /* set ^X prefixing pending */
  819.  
  820. {
  821.     prefix |= CTLX;
  822.     prenum = n;
  823.     predef = f;
  824.     return(TRUE);
  825. }
  826.  
  827. PASCAL NEAR unarg()    /* dummy function for binding to universal-argument */
  828. {
  829. }
  830.  
  831. /*    bytecopy:    copy a string...with length restrictions
  832.             ALWAYS null terminate
  833. */
  834.  
  835. char *PASCAL NEAR bytecopy(dst, src, maxlen)
  836.  
  837. char *dst;    /* destination of copied string */
  838. char *src;    /* source */
  839. int maxlen;    /* maximum length */
  840.  
  841. {
  842.     char *dptr;    /* ptr into dst */
  843.  
  844.     dptr = dst;
  845.     while (*src && (maxlen-- > 0))
  846.         *dptr++ = *src++;
  847.     *dptr = 0;
  848.     return(dst);
  849. }
  850.  
  851. /*****        Compiler specific Library functions    ****/
  852.  
  853. #if    RAMSIZE
  854. /*    These routines will allow me to track memory usage by placing
  855.     a layer on top of the standard system malloc() and free() calls.
  856.     with this code defined, the environment variable, $RAM, will
  857.     report on the number of bytes allocated via malloc.
  858.  
  859.     with SHOWRAM defined, the number is also posted on the
  860.     end of the bottom mode line and is updated whenever it is changed.
  861. */
  862.  
  863. #undef    malloc
  864. #undef    free
  865.  
  866. char *allocate(nbytes)    /* allocate nbytes and track */
  867.  
  868. unsigned nbytes;    /* # of bytes to allocate */
  869.  
  870. {
  871.     char *mp;    /* ptr returned from malloc */
  872.     char *malloc();
  873.     FILE *track;    /* malloc track file */
  874.  
  875.     mp = malloc(nbytes);
  876.  
  877. #if    RAMTRCK
  878.     track = fopen("malloc.dat", "a");
  879.     fprintf(track, "Allocating %u bytes at %u:%u\n", nbytes,
  880.             FP_SEG(mp), FP_OFF(mp));
  881.     fclose(track);
  882. #endif
  883.  
  884.     if (mp) {
  885. #if    0
  886.         envram += nbytes;
  887. #else
  888.         envram += 1;
  889. #endif
  890. #if    RAMSHOW
  891.         dspram();
  892. #endif
  893.     }
  894.  
  895.     return(mp);
  896. }
  897.  
  898. release(mp)    /* release malloced memory and track */
  899.  
  900. char *mp;    /* chunk of RAM to release */
  901.  
  902. {
  903.     unsigned *lp;    /* ptr to the long containing the block size */
  904. #if    RAMTRCK
  905.     FILE *track;    /* malloc track file */
  906.  
  907.     track = fopen("malloc.dat", "a");
  908.     fprintf(track, "Freeing %u:%u\n",
  909.             FP_SEG(mp), FP_OFF(mp));
  910.     fclose(track);
  911. #endif
  912.  
  913.     if (mp) {
  914.         /* update amount of ram currently malloced */
  915. #if    0
  916.         lp = ((unsigned *)mp) - 1;
  917.         envram -= (long)*lp - 2;
  918. #else
  919.         envram -= 1;
  920. #endif
  921.         free(mp);
  922. #if    RAMSHOW
  923.         dspram();
  924. #endif
  925.     }
  926. }
  927.  
  928. #if    RAMSHOW
  929. dspram()    /* display the amount of RAM currently malloced */
  930.  
  931. {
  932.     char mbuf[20];
  933.     char *sp;
  934.  
  935.     TTmove(term.t_nrow - 1, 70);
  936. #if    COLOR
  937.     TTforg(7);
  938.     TTbacg(0);
  939. #endif
  940.     sprintf(mbuf, "[%lu]", envram);
  941.     sp = &mbuf[0];
  942.     while (*sp)
  943.         TTputc(*sp++);
  944.     TTmove(term.t_nrow, 0);
  945.     movecursor(term.t_nrow, 0);
  946. }
  947. #endif
  948. #endif
  949.